package com.example.qixinshangwutong.view.fragment;

import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.qixinshangwutong.R;
import com.example.qixinshangwutong.model.follows.ObjectAdapter;
import com.example.qixinshangwutong.model.follows.ObjectModel;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

import cn.leancloud.LCObject;
import cn.leancloud.LCQuery;
import cn.leancloud.LCUser;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;


public class FollowFragment extends Fragment {
    RecyclerView recyclerView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View root=inflater.inflate(R.layout.fragment_follow, container, false);
        initView(root);
        return root;
    }
    private void initView( View root){
        recyclerView=root.findViewById(R.id.recycle_gz);
        LinearLayoutManager llm=new LinearLayoutManager(getContext());
        recyclerView.setLayoutManager(llm);
    }
    private void follows(){
        List<ObjectModel> data=new ArrayList<>();
        ObjectAdapter adapter=new ObjectAdapter(getActivity());
        adapter.setCompanyData(data);
        recyclerView.setAdapter(adapter);
        LCUser currentUser=LCUser.getCurrentUser();
        if (currentUser!=null){
            LCQuery<LCObject> query=new LCQuery<>("focus");
            query.whereEqualTo("id",currentUser.getObjectId());
            query.findInBackground()
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Observer<List<LCObject>>() {
                        @Override
                        public void onSubscribe(@NotNull Disposable d) {

                        }

                        @Override
                        public void onNext(@NotNull List<LCObject> lcObjects) {
                            for (int i = 0; i < lcObjects.size(); i++) {
                                ObjectModel model=new ObjectModel();
                                model.setImageurl(lcObjects.get(i).get("imageurl").toString());
                                model.setTitle(lcObjects.get(i).get("tittle").toString());
                                model.setUrl(lcObjects.get(i).get("url").toString());
                                data.add(model);
                            }
                            adapter.notifyDataSetChanged();
                        }

                        @Override
                        public void onError(@NotNull Throwable e) {

                        }

                        @Override
                        public void onComplete() {

                        }
                    });
        }
    }

    @Override
    public void onResume() {

        super.onResume();
        follows();
    }
}